package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.VpnServiceReqDTO;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.VpnBusinessNameAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 云聚节点-查询VPN业务名称
 *
 * @author LIFF
 */
@Service
@AttrHandler(VpnBusinessNameAttrHandler.class)
public class VpnBusinessNameAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

  @Autowired OperateInstAttrService operateInstAttrService;

  @Autowired IInfServiceBus infServiceBus;

  @Override
  public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
    // VPN业务名称
    KiteAttr attr1 = inst.getAttr("200001741");
    // 加入已有VPN业务：是
    if ("1_0000106".equals(attr.getValue())) {
      VpnServiceReqDTO reqDTO = new VpnServiceReqDTO();
      reqDTO.setCustId(data.getCustId());
      reqDTO.setQueryType("0");

      List<KiteAttrValue> list = new ArrayList<KiteAttrValue>();
      attr1.setAttrValueTypeId("98A");
      List vpnBusinessNamesList = infServiceBus.findVpnBusinessNamesList(reqDTO);
      vpnBusinessNamesList.stream()
          .forEach(
              map -> {
                KiteAttrValue attrValue = new KiteAttrValue();
                attrValue.setAttrValueName(KiteMapUtils.getStrValue((Map) map, "serviceName"));
                attrValue.setAttrValue(KiteMapUtils.getStrValue((Map) map, "serviceName"));
                attrValue.setAttrValueDesc(KiteMapUtils.getStrValue((Map) map, "serviceName"));
                list.add(attrValue);
              });
      attr1.setAttrValues(list);
    } else {
      attr1.setAttrValueTypeId("98C");
      attr1.setValue("");
    }
    operateInstAttrService.setAttrValue(
        inst, "200001741", attr1.getValue(), data.getSceneInstId(), null);
  }

  @Override
  public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {}
}
